package com.zzstm.staservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzstm.commonUtils.R;
import com.zzstm.staservice.client.UcenterClient;
import com.zzstm.staservice.entity.StatisticsDaily;
import com.zzstm.staservice.mapper.StatisticsDailyMapper;
import com.zzstm.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-11-21
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    //查询某天注册人数
    @Override
    public void countRegister(String day) {
        //先删除记录再添加
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);


        R countRegister = ucenterClient.countRegister(day);
        Integer count = (Integer) countRegister.getData().get("count");
        StatisticsDaily daily = new StatisticsDaily();
        daily.setDateCalculated(day);
        daily.setRegisterNum(count);    //注册人数
        //模拟数据
        daily.setCourseNum(RandomUtils.nextInt(100,200));
        daily.setLoginNum(RandomUtils.nextInt(100,200));
        daily.setVideoViewNum(RandomUtils.nextInt(100,200));
        baseMapper.insert(daily);
    }

    //图表显示，返回两部分数据，日期的json数组，数量的json数组
    @Override
    public Map<String, Object> showData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);
        List<StatisticsDaily> dailyList = baseMapper.selectList(wrapper);
        //创建2个集合，分别封装2个字段的值
        List<String> dateList = new ArrayList<>();
        List<Integer> numList = new ArrayList<>();
        for(StatisticsDaily s : dailyList){
            dateList.add(s.getDateCalculated());
            switch (type){
                case "register_num":
                    numList.add(s.getRegisterNum());
                    break;
                case "login_num":
                    numList.add(s.getLoginNum());
                    break;
                case "video_view_num":
                    numList.add(s.getVideoViewNum());
                    break;
                case "course_num":
                    numList.add(s.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("dateList",dateList);
        map.put("numList",numList);
        return map;
    }
}
